package jgl.vajra.operator.widget.dialogfragment;

import android.content.Context;
import android.content.DialogInterface;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;

import jgl.vajra.operator.R;
import jgl.vajra.operator.widget.bean.UpdateApkBean;
import jgl.vajra.operator.databinding.DialogFragmentUpdateVersionBinding;
import jgl.vajra.operator.widget.utils.ToastUtils;

/**
 * Created by Administrator on 2019/4/16.
 */

public class UpdateVersionDialogFragment extends BaseDialogFragment<DialogFragmentUpdateVersionBinding> {
    private UpdateApkBean updateApkBean;
    private long exitTime;

    @Override
    public void initView(Context context, View rootView, AttributeSet attrs) {
        mBinding.setView(this);
        mBinding.setData(updateApkBean);
    }

    @Override
    public void initData(Context context) {
        setCancelable(!updateApkBean.forceUpdate);
    }

    @Override
    public void initEvent(Context context) {
        getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface dialog, int keyCode,
                                 KeyEvent event) {

                if (updateApkBean.forceUpdate) {
                    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                        if (System.currentTimeMillis() - exitTime > 2000) {
                            ToastUtils.showShort(R.string.Click_again_to_return_desktop);
                            exitTime = System.currentTimeMillis();
                        } else {
                            System.exit(0);
                        }
                        return true;
                    }
                }

                return false;
            }
        });
    }

    @Override
    public boolean getCancelOutside() {
        return !updateApkBean.forceUpdate;
    }

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_fragment_update_version;
    }

    public void initDialogConfig(UpdateApkBean updateApkBean) {
        this.updateApkBean = updateApkBean;
    }

    public void leftClick() {

    }

    public void update() {

    }

    @Override
    public int getGravity() {
        return Gravity.CENTER;
    }



}
